/**
 * Created by zhubuqing on 2018/4/16.
 */
var url = decodeURI(location.href); // 获取URL

var getTeacherByIdUrl = baseUrl + "/common/famousTeacher/getTeacherById";
var getFamousTeacherByTeacherIdUrl = baseUrl + "/common/famousTeacher/getFamousTeacherByTeacherId";
var getTeacherResourceUrl = baseUrl + "/common/famousTeacher/getTeacherResource";
var searchResourceUrl = baseUrl + "/common/famousTeacher/searchResource";

var teacherName = "";

$(function () {
    var teacherId = getQueryString(url)[0];

    // 获取教师
    getTeacherById(teacherId);
});

// 根据ID获取教师信息
function getTeacherById(teacherId) {
    var data = {
        id: teacherId
    };

    commonAjax(getTeacherByIdUrl, data, "getTeacherByIdResult", "GET");
}
function getTeacherByIdResult(res) {
    console.log(res);

    var resultData = res.obj;
    $("#famousTeacherLogo").attr("src", resultData.pic)
    $("#famousTeacherName").html(resultData.teacherName);
    $("#collegeAndPositional").html(resultData.schoolName + "，" + changeTeacherPositionalEnum(resultData.positional));

    teacherName = resultData.teacherName;

    // 获取名师信息
    getFamousTeacherByTeacherId(resultData.id);

    // 获取学术著作
    $("#toXqListXszz").html('<a href="xq_list.html?type=mingShi_xueShuZhuZuo&teacherId=' + resultData.id + '">更多</a>');
    getTeacherResource(resultData.id, 1);

    // 获取教学成果
    $("#toXqListJxcg").html('<a href="xq_list.html?type=mingShi_jiaoXueChengGuo&teacherId=' + resultData.id + '">更多</a>');
    getTeacherResource(resultData.id, 2);

    // 获取学术论文
    $("#toXqListXslw").html('<a href="xq_list.html?type=mingShi_xueShuLunWen&teacherId=' + resultData.id + '">更多</a>');
    getTeacherResource(resultData.id, 3);

    // 获取我的视频
    $("#toXqListWdsp").html('<a href="xq_list.html?type=mingShi_woDeShiPin&teacherId=' + resultData.id + '">更多</a>');
    searchResource(resultData.id);

    $("#toXqListWlzy").html('<a href="xq_list.html?type=mingShi_wangLuoZiYuan&teacherId=' + resultData.id + '">更多</a>');
    // 获取网络资源
    getTeacherResource(resultData.id, 4);
}
function getFamousTeacherByTeacherId(teacherId) {
    var data = {
        teacherId: teacherId
    };

    commonAjax(getFamousTeacherByTeacherIdUrl, data, "getFamousTeacherByTeacherIdResult", "GET");
}
function getFamousTeacherByTeacherIdResult(res) {
    if (res.obj.brief == null) {
        $("#famousTeacherBrief").html(null);
    } else {
        $("#famousTeacherBrief").html(res.obj.brief);
    }
}

// 获取教师资源
function getTeacherResource(teacherId, type) {
    var data = {
        teacherId: teacherId,
        teacherResourceTypeId: type,
        page: 0,
        size: 5
    }

    commonAjax(getTeacherResourceUrl, data, "getTeacherResourceResult", "GET", type);
}
function getTeacherResourceResult(res, type) {
    var resultData = res.obj.content;

    var teacherResourceHtml = "";

    if (type == 1) { // 学术著作
        for (var i = 0; i < resultData.length; i++) {
            /* teacherResourceHtml += "<a onclick=toXqNormal('" + resultData[i].id + "','mingShi_xueShuZhuZuo')><li>" + resultData[i].title + "</li></a>";*/
            teacherResourceHtml += "<li>" + resultData[i].title + "</li>";
        }

        $("#teacherResourceXueShuZhuZuo").html(teacherResourceHtml);
    } else if (type == 2) { // 教学成果
        for (var i = 0; i < resultData.length; i++) {
            /* teacherResourceHtml += "<a onclick=toXqNormal('" + resultData[i].id + "','mingShi_jiaoXueChengGuo')><li>" + resultData[i].title + "</li></a>";*/
            teacherResourceHtml += "<li>" + resultData[i].title + "</li>";
        }

        $("#teacherResourceJiaoXueChengGuo").html(teacherResourceHtml);
    } else if (type == 3) { // 学术论文
        for (var i = 0; i < resultData.length; i++) {
            /* teacherResourceHtml += "<a onclick=toXqNormal('" + resultData[i].id + "','mingShi_xueShuLunWen')><li class='clearfix'>";
             teacherResourceHtml += "<div class='lunwen'>";
             teacherResourceHtml += "<p class='bold'>" + resultData[i].title + "</p>";
             teacherResourceHtml += "<p>" + teacherName + "</p>";
             teacherResourceHtml += "<p>" + limitLength(77, resultData[i].text) + "</p>";
             teacherResourceHtml += "<p class='important'>关键字：" + resultData[i].keyWord + "</p>";
             teacherResourceHtml += "</div>";
             teacherResourceHtml += "</li></a>";*/
            teacherResourceHtml += "<li class='clearfix'>";
            teacherResourceHtml += "<div class='lunwen'>";
            teacherResourceHtml += "<p class='bold'>" + resultData[i].title + "</p>";
            teacherResourceHtml += "<p>" + resultData[i].text + "</p>";
            teacherResourceHtml += "<p class='important'>关键字：" + resultData[i].keyWord + "</p>";
            teacherResourceHtml += "</div>";
            teacherResourceHtml += "</li>";
        }

        $("#teacherResourceXueShuLunWen").html(teacherResourceHtml);
    } else if (type == 4) { // 网络资源
        for (var i = 0; i < resultData.length; i++) {
            teacherResourceHtml += '<a href="' + resultData[i].url + '" target="_blank"><div class="col-md-6 border-bottom">';
            teacherResourceHtml += '<img src="../img/blue.png" alt="" class="pull-left">';
            teacherResourceHtml += '<p class="wordname">' + resultData[i].title + '</p>';
            teacherResourceHtml += '</div></a>';
            /* teacherResourceHtml += '<div class="col-md-6 border-bottom">';
             teacherResourceHtml += '<img src="../img/red.png" alt="" class="pull-left">';
             teacherResourceHtml += '<p class="wordname">' + resultData[i].title + '</p>';
             teacherResourceHtml += '</div>';*/
        }

        $("#teacherResourceWangLuoZiYuan").html(teacherResourceHtml);
    }
}

// 获取我的视频
function searchResource(teacherId) {
    var data = {
        page: 0,
        size: 10,
        userId: teacherId
    }

    commonAjax(searchResourceUrl, data, "searchResourceResult", "GET");
}
function searchResourceResult(res) {
    var resultData = res.obj.content;

    var resourceWoDeShiPinHtml = "";

    for (var i = 0; i < resultData.length; i++) {
        resourceWoDeShiPinHtml += "<a onclick=toXqVideo('" + resultData[i].id + "','mingShi_woDeShiPin')><div class='imgBoxList'>";
        resourceWoDeShiPinHtml += "<img src='" + resultData[i].pic + "' alt=''>";
        resourceWoDeShiPinHtml += "<p>" + resultData[i].title + "</p>";
        resourceWoDeShiPinHtml += "<p class='time'>" + toHourMinute(resultData[i].videoTime) + "</p>";
        resourceWoDeShiPinHtml += "</div></a>";
    }

    $("#resourceWoDeShiPin").html(resourceWoDeShiPinHtml);
}

/******************************************** util ********************************************/

function getQueryString(url) {
    var useUrl = url.split("?")[1];
    var params = new Array();
    params = useUrl.split("&");
    var needParams = new Array();
    for (var i = 0; i < params.length; i++) {
        var needParam = params[i].split("=")[1];
        needParams.push(needParam);
    }
    return needParams;
}

// 教师职称枚举转换
function changeTeacherPositionalEnum(positional) {
    if (positional == "PROFESSOR") {
        return "教授";
    } else if (positional == "ASSOCIATE_PROFESSOR") {
        return "副教授";
    } else if (positional == "LECTURER") {
        return "讲师"
    } else if (positional == "INSTRUCTOR") {
        return "辅导员";
    } else if (positional == "教授") {
        return "PROFESSOR";
    } else if (positional == "副教授") {
        return "ASSOCIATE_PROFESSOR";
    } else if (positional == "讲师") {
        return "LECTURER";
    } else if (positional == "辅导员") {
        return "INSTRUCTOR";
    }
}

// 限制字符串长度
function limitLength(wantLength, str) {
    var returnStr = "";
    var realLength = 0;
    var len = (str != null) ? str.length : 0;
    var charCode = -1;
    for (var i = 0; i < len; i++) {
        if (realLength < wantLength) {
            charCode = str.charCodeAt(i);
            if (charCode >= 0 && charCode <= 128)
                realLength += 1;
            else
                realLength += 2;
            returnStr += str.charAt(i);
        } else {
            returnStr += "...";
            break;
        }
    }
    return returnStr;
}

// 跳转至详情
function toXqNormal(followId, type) {
    var followId = followId;
    var theType = type;
    sessionStorage.setItem("followId", JSON.stringify(followId));
    sessionStorage.setItem("type", JSON.stringify(theType));
    self.location.href = "xq.html";
}

// 跳转至视频
function toXqVideo(followId, type) {
    var followId = followId;
    var theType = type;
    sessionStorage.setItem("followId", JSON.stringify(followId));
    sessionStorage.setItem("type", JSON.stringify(theType));
    window.open("video.html");
}